package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.PmsProductAttributeCategoryParam;
import com.macro.mall.model.PmsProductAttributeCategory;
import com.macro.mall.service.PmsProductAttributeCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "PmsProductAttributeCategoryController" ,description = "商品属性分类管理")
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @ApiOperation("分页获取所有商品属性分类")
    @RequestMapping(value = "/category/list" , method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getList(@RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                                                         @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryService.selectByPage(pageSize,pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("获取单个商品属性分类信息")
    @RequestMapping(value = "/category/{id}",method = RequestMethod.GET)
    public CommonResult<PmsProductAttributeCategory> getOne(@PathVariable("id") Long id){
        PmsProductAttributeCategory attributeCategory = pmsProductAttributeCategoryService.selectById(id);
        return CommonResult.success(attributeCategory);
    }
    @ApiOperation("添加商品属性分类")
    @RequestMapping(value = "/category/create", method = RequestMethod.POST)
    public CommonResult insertName(@RequestBody PmsProductAttributeCategoryParam attributeCategory){
        int a = pmsProductAttributeCategoryService.insert(attributeCategory);
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(attributeCategory);
        }else {
            commonResult = CommonResult.failed("添加失败");
        }
        return commonResult;
    }
    @ApiOperation("删除单个商品属性分类")
    @RequestMapping(value = "/category/delete/{id}",method = RequestMethod.GET)
    public CommonResult delete(@PathVariable("id") Long id){
        int a = pmsProductAttributeCategoryService.delete(id);
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(id);
        }else {
            commonResult = CommonResult.failed("删除失败");
        }
        return commonResult;
    }
    @ApiOperation("修改商品属性分类")
    @RequestMapping(value = "/category/update/{id}",method = RequestMethod.POST)
    public CommonResult update(@PathVariable("id") Long id,@RequestBody PmsProductAttributeCategoryParam attributeCategory){
        attributeCategory.setId(id);
        int a = pmsProductAttributeCategoryService.update(attributeCategory);
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(id);
        }else {
            commonResult = CommonResult.failed("删除失败");
        }
        return commonResult;
    }

    @ApiOperation("获取所有商品属性分类及其下属")
    @RequestMapping(value = "/category/list/withAttr" , method = RequestMethod.GET)
    public CommonResult<List<PmsProductAttributeCategory>> getAll(){
        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryService.selectAll();
        return CommonResult.success(list);
    }
}
